﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using System.Net.Mail;
using System.Net.Configuration;
using System.Net;
using System.Configuration;
using System.Security.Cryptography;
using System.Web.Configuration;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace Website
{
    public class SendEmail
    {
        public void SendMail(string email, string pass)
        {
            Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

            MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
            int port = 0;
            string host = "";
            string password = "";
            string username = "";
            string from = "";

            if (mailSettings != null)
            {
                port = mailSettings.Smtp.Network.Port;
                host = mailSettings.Smtp.Network.Host;
                password = mailSettings.Smtp.Network.Password;
                username = mailSettings.Smtp.Network.UserName;
                from = mailSettings.Smtp.From;
            }

            MailMessage msg = new MailMessage();

            msg.To.Add(email);

            msg.From = new System.Net.Mail.MailAddress(from, "We Company", Encoding.UTF8);
            msg.Subject = "We Company";
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "Your Password : " + "<br/>" + "<b>" + pass + "</b>";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = System.Net.Mail.MailPriority.High;

            //Add the Creddentials

            SmtpClient client = new SmtpClient(host, port);
            client.Credentials = new NetworkCredential(username, password);//dat vao file config        
            client.EnableSsl = true;

            try
            {
                client.Send(msg);
                //Response.Write("Send Mail OK");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Console.Write(ex);
                //Response.Write("Send Mail Error: " + ex.Message);
            }
        }

        public void SendMailMessage(string Toemail, string Subject,String Name,String CustomerEmail,String Content)
        {
            Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);

            MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
            int port = 0;
            string host = "";
            string password = "";
            string username = "";
            string from = "";

            if (mailSettings != null)
            {
                port = mailSettings.Smtp.Network.Port;
                host = mailSettings.Smtp.Network.Host;
                password = mailSettings.Smtp.Network.Password;
                username = mailSettings.Smtp.Network.UserName;
                from = mailSettings.Smtp.From;
            }

            MailMessage msg = new MailMessage();

            msg.To.Add(Toemail);

            msg.From = new System.Net.Mail.MailAddress(from, "Thư khách hàng", Encoding.UTF8);
            msg.Subject = Subject;
            msg.SubjectEncoding = System.Text.Encoding.UTF8;
            msg.Body = "<p>Khách hàng : </p>" + Name + "<p>Điện thoại :</p>" + "<p>Thư điện tử:</p>" + CustomerEmail + "<p>Nội dung:</p><p>" + Content + "</p>";
            msg.BodyEncoding = System.Text.Encoding.UTF8;
            msg.IsBodyHtml = true;
            msg.Priority = System.Net.Mail.MailPriority.High;

            //Add the Creddentials

            SmtpClient client = new SmtpClient(host, port);
            client.Credentials = new NetworkCredential(username, password);//dat vao file config        
            client.EnableSsl = true;

            try
            {
                client.Send(msg);
                //Response.Write("Send Mail OK");
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Console.Write(ex);
                //Response.Write("Send Mail Error: " + ex.Message);
            }
        }
    }
}
